<template>
  <div class="app-container">
    <div class="filter-container">
      <div class="lz-tips">
        <label>历史推送:</label>
      </div>
      <el-date-picker
        v-model="sendHistoryDate"
        @change="search()"
        :default-time="['00:00:00']"
        type="datetimerange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      >
      </el-date-picker>

      <div class="float-right">
        <el-button
          type="primary"
          class="send_new_btn"
          @click="addNewMessage()"
          v-show="permission_roles.fetchYouMengPushData"
        >新建推送(友盟+APP)</el-button>
      </div>
    </div>
    <div class="table_container">
      <el-table
        border
        v-loading="tableloading"
        :data="youMengPushData"
        highlight-current-row
        style="width: 100%"
      >
        <el-table-column
          type="index"
          align="center"
          fixed
        >
        </el-table-column>
        <el-table-column
          property="createTime"
          :formatter="formatterTime"
          label="推送时间"
          width="100"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="ticker"
          label="模版名称"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="typeName"
          label="推送类型"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="title"
          label="推送标题"
          align="center"
        >
        </el-table-column>
        <el-table-column
          property="text"
          :formatter="formatterWordsNumber"
          label="推送内容"
          align="center"
          width="200"
        >
        </el-table-column>
        <el-table-column
          property="count"
          label="发送数量"
          align="center"
        >
        </el-table-column>
        <el-table-column
          label="详情"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              @click="goView(scope.row,2)"
              type="text"
              size="small"
            >查看</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div
        class="Pagination"
        style="text-align: left;margin-top: 10px;"
      >
        <el-pagination
          v-if="pageshow"
          @current-change="currentChange"
          :current-page="currentPage"
          :page-size="limit"
          layout="total, prev, pager, next"
          :total="count"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { mapGetters } from "vuex";
import { fetchYouMengPushHistoryData } from "@/api/other";
import { formatDateTime } from "@/utils/common";

export default {
  data() {
    return {
      count: 0,
      limit: 20,
      currentPage: 1,
      tableloading: false,
      sendHistoryDate: [],
      youMengPushData: [],
      pageshow: true
    };
  },
  created() {
    this.getYouMengPushDataData();
  },
  methods: {
    currentChange(val) {
      this.currentPage = val;
      this.getYouMengPushDataData();
    },
    search() {
      this.currentPage = 1;
      this.pageshow = false;
      this.getYouMengPushDataData();
      this.$nextTick(() => {
        this.pageshow = true;
      });
    },
    async getYouMengPushDataData() {
      let startTime = "";
      let endTime = "";
      if (this.sendHistoryDate == null || this.sendHistoryDate.length == 0) {
        startTime = "";
        endTime = "";
      } else {
        startTime = formatDateTime(this.sendHistoryDate[0]);
        endTime = formatDateTime(this.sendHistoryDate[1]);
      }
      this.tableloading = true;
      const sendData = await fetchYouMengPushHistoryData({
        pageNo: this.currentPage,
        pageSize: this.limit,
        startTime: startTime,
        endTime: endTime
      });
      this.tableloading = false;
      this.youMengPushData = [];
      this.count = sendData.data.total;
      if (sendData.data.list) {
        sendData.data.list.forEach(item => {
          const tableData = {};
          tableData.createTime = item.createTime;
          tableData.title = item.title;
          tableData.typeId = item.typeId;
          tableData.text = item.text;
          tableData.count = item.count;
          tableData.ticker = item.ticker;
          tableData.typeName = item.typeName;
          this.youMengPushData.push(tableData);
        });
      }
    },
    formatterTime(row) {
      if (row.createTime) {
        return formatDateTime(row.createTime);
      } else {
        return "";
      }
    },
    formatterWordsNumber(row) {
      if (row.text.length > 10) {
        const word = row.text;
        return word.slice(0, 10) + "...";
      } else {
        return row.text;
      }
    },
    //typeId  1短信2友盟3站内
    addNewMessage() {
      this.$router.push({
        path: "/messageManage/outSitePush/newOutSite",
        query: { typeId: "2" }
      });
    },
    //templateId 1是短信
    goView(data, i) {
      this.$router.push({
        path: "/messageManage/outSitePush/messageView",
        query: {
          createTime: formatDateTime(data.createTime),
          templateId: i
        }
      });
    }
  },
  computed: {
    ...mapGetters(["permission_roles"])
  },
  watch: {
    $route() {
      this.getYouMengPushDataData();
    }
  }
};
</script>
<style lang="less" scoped>
.last {
  text-align: right;
}
</style>
